﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Telerik.Web.UI;
using System.IO;
using KPIS.GERP.ESS.BLL;
using KPIS.GERP.ESS.MODEL;

namespace KPIS.GERP.ESS.APP.App_UI.Forms
{
    public partial class RequestAbsences : System.Web.UI.Page
    {
        private B_HrmRequest reqBLL = new B_HrmRequest();
        private B_MessageSend sendBLL = new B_MessageSend();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewGrid();
            }            
        }
        public void ViewGrid()
        {
            LoadDataGrid();
            PanelFilter.Visible = true;
            PanelDetail.Visible = false;
            reqAbsence1.Visible = false;

            if (PanelDetail.Visible)
                lblHeader.Text = "แบบฟอร์มการลา";
            else
                lblHeader.Text = "รายการใบลา";
        }
        public void LoadDataGrid()
        {
            DataSet ds = reqBLL.GetRequestAbsences((int?)null, Int32.Parse(Session["EmpSeq"].ToString()), "");
            DataTable dt = ds.Tables[0];

            RadGrid1.DataSource = dt;
            RadGrid1.DataBind();
        }
        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "NewInsert")
            {
                //int seq = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["send_seq"]);

                PanelFilter.Visible = false;
                PanelDetail.Visible = true;
                reqAbsence1.Visible = true;
                reqAbsence1.LoadDataEditInsert(0);
            }
            else if (e.CommandName == "Delete")
            {
                int SendSequence = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["send_seq"]);
                int Sequence = Int32.Parse(RadGrid1.Items[e.Item.ItemIndex]["absence_seq"].Text);

                // ลบ Table AbsenceRequest ก่อน
                reqBLL.DeleteAbsence(Sequence, Int32.Parse(Session["EmpSeq"].ToString()));
                
                //ลบ Table MessageSend
                M_MessageSend info = new M_MessageSend();
                info.send_seq = SendSequence;
                info.updated_by = Int32.Parse(Session["EmpSeq"].ToString());
                info.updated_when = DateTime.Now;
                sendBLL.Delete(info);
            }
            else if (e.CommandName == "ShowDetail" && e.Item is GridDataItem)
            {
                //int Sequence = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["absence_seq"]);

                int Sequence = Int32.Parse(RadGrid1.Items[e.Item.ItemIndex]["absence_seq"].Text);

                PanelFilter.Visible = false;
                PanelDetail.Visible = true;
                reqAbsence1.Visible = true;
                reqAbsence1.LoadDataView(Sequence);
            }
            LoadDataGrid();
        }
        protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            LoadDataGrid();
        }
    }
}
